#include <../inc/aht20.h>


int aht20_init(int fd){

    uint8_t init_cmd[3]={0xBE, 0x08, 0x00};

    if(write(fd,init_cmd,sizeof(init_cmd))!=sizeof(init_cmd)){
        printf("%d\n",(write(fd,init_cmd,sizeof(init_cmd))));
        perror("failed to init AHT20\n");
        return (-1);
    }

    usleep(10000);
    return 0;
}

int aht20_measure(int fd){
    uint8_t measure_cmd[3]={0xAC, 0x33, 0x00};

    if(write(fd,measure_cmd,sizeof(measure_cmd))!=sizeof(measure_cmd)){
        perror("failed to trigger measure\n");
        return (-1);
    }

    return 0;
}

int aht20_read_data(int fd,float *temp,float *humi){
    uint32_t temp_data = 0, humi_data = 0;
    uint8_t data[6]={0};
    int retry=10;

    while(retry--){
        if(read(fd,data,1)!=1){
            perror("failed read data\n");
            return (-1);
        }

        if(!(data[0]&0x80)){
            break;
        }

        usleep(20000);
    }
    if(retry<=0){
            printf("time out\n");
            return (-1);
        }

        if(read(fd,data,sizeof(data))!=sizeof(data)){
            return (-1);
        }

        humi_data = data[1];
        humi_data <<= 8;
        humi_data |= data[2];
        humi_data <<= 8;
        humi_data |= data[3];
        humi_data >>= 4;
        
        *humi = (float)humi_data / 1048576 * 100;

        temp_data = data[3];
        temp_data <<= 8;
        temp_data |= data[4];
        temp_data <<= 8;
        temp_data |= data[5];
        temp_data &= 0x0fffff;
        
        *temp = (float)temp_data / 1048576 * 200 - 50;

        return 0;
}

int aht20_get_data(float *humi,float *temp){
    int fd;
    float humi_get;
    float temp_get;
    char tx_buf[]="HELLO LOONGNIX\n";
    char rx_buf[256]={0};

    if((fd=open(I2C_DEVICE,O_RDWR))<0){
        perror("Failed to open i2c device\n");
        return (-1);
    }

    if(ioctl(fd,I2C_SLAVE,AHT20_ADDR)<0){
        perror("Failed to set slave addr\n");

        close(fd);
        return (-1);
    }

    if(aht20_init(fd)<0){
        close(fd);
        return (-1);
    }

    if(aht20_measure(fd)<0){
        close(fd);
        return (-1);
    }

    if(aht20_read_data(fd,&temp_get,&humi_get)<0){
        close(fd);
        return (-1);
    }
    *humi=humi_get;
    *temp=temp_get;
    printf("temp:%.2f,humi:%.2f\n",temp_get,humi_get);
    close(fd);

    return 0;
}

int aht20_get_data_2(float *humi,float *temp){
    int fd;
    float humi_get;
    float temp_get;
    char tx_buf[]="HELLO LOONGNIX\n";
    char rx_buf[256]={0};

    if((fd=open(I2C_DEVICE_2,O_RDWR))<0){
        perror("Failed to open i2c device\n");
        return (-1);
    }

    if(ioctl(fd,I2C_SLAVE,AHT20_ADDR)<0){
        perror("Failed to set slave addr\n");

        close(fd);
        return (-1);
    }

    if(aht20_init(fd)<0){
        close(fd);
        return (-1);
    }

    if(aht20_measure(fd)<0){
        close(fd);
        return (-1);
    }

    if(aht20_read_data(fd,&temp_get,&humi_get)<0){
        close(fd);
        return (-1);
    }
    *humi=humi_get;
    *temp=temp_get;
    printf("temp:%.2f,humi:%.2f\n",temp_get,humi_get);
    close(fd);

    return 0;
}